package com.itheima.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer_Topic1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.17.139");//默认为127.0.0.1
        factory.setPort(5672);
        factory.setVirtualHost("/itcast");//虚拟机
        factory.setUsername("zwy");//用户名
        factory.setPassword("123456");//密码
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        //队列名，是否持久化，是否独占，是否自动删除，参数
//        channel.queueDeclare("work_queues",true,false,false,null);

        String queue1="test_topic1";
        String queue2="test_topic2";


        Consumer consumer=new DefaultConsumer(channel){
            //回调方法
            /*
            1.标识
            2.交换机
            3.properties 配置信息
            4.body 数据
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                System.out.println("consumerTag: "+consumerTag);
//                System.out.println("envelope: "+envelope);
//                System.out.println("properties: "+properties);
                System.out.println("body: "+new String(body));
                System.out.println("保存数据库");
            }
        };
        channel.basicConsume(queue1,true,consumer);
    }
}
